package cn.ywang.pattern.singleton.lazy;

/**
 * 内部静态类懒汉式单例
 *
 * @author BeatificWang
 * @date 2019/3/10 14:40
 */
public class InnerLazy {

	/**
	 * 防止反射重复创建，破坏单例
	 */
	private InnerLazy() {
		if (null != LazyHolder.INSTANCE) {
			throw new RuntimeException("不允许重复创建单例");
		}
	}

	/**
	 * 内部静态类保障单例，当调用该方法时才加载内部类，避免资源浪费
	 *
	 * @return
	 */
	public static InnerLazy getInstance() {
		return LazyHolder.INSTANCE;
	}

	/**
	 * 防止反序列化破坏单例
	 *
	 * @return
	 */
	public InnerLazy readResolve() {
		return LazyHolder.INSTANCE;
	}

	private static class LazyHolder {
		private static final InnerLazy INSTANCE = new InnerLazy();
	}
}
